/**
 * 
 */
package lobby.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.common.json.JSON;
import com.alibaba.dubbo.config.annotation.Service;
import com.google.gson.Gson;

import lobby.dao.mapper.ILobbyMapper;
import lobby.entity.douban.Movie;
import lobby.entity.douban.Subject;
import lobby.entity.douban.Subjects;
import lobby.service.ILobbyService;
import lobby.service.net.HttpUtils;

/**
 *
 * @author Eric
 */
@Service
public class LobbyService implements ILobbyService {
	
	@Autowired
	HttpUtils http;
	
	@Autowired
	ILobbyMapper lobbyMapper;
	
	@Override
	public String test() {
		String subjectresp = "";
		try {
			subjectresp = http.request("https://api.douban.com/v2/movie/subject/1764796", "");
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@Override
	public void initMovie(String direct1) {
		String txt = read();
		for(String s : txt.split("\n")) {
			try {
				
			String direct = s.replace(s.substring(0, s.indexOf(".")+1), "@").replace("@", "");
			System.out.println(unicode(direct));
			init(direct);
			Thread.sleep(5000);
			
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	private void init(String direct) {
		
		String subjectresp = "";
		try {
			subjectresp = http.request("https://api.douban.com/v2/movie/search?q=" + direct, "");
			System.out.println(subjectresp);
			Subjects subjects = JSON.parse(subjectresp, Subjects.class);
			List<Subject> subjectList = subjects.getSubjects();
			
			for(int i=0; i<subjectList.size(); i++) {
				Subject s = JSON.parse(new Gson().toJson(subjectList.get(i)), Subject.class);
				lobbyMapper.save(
						Movie.builder()
							.id(s.getId())
							.originalTitle(s.getOriginal_title())
							.title(s.getTitle())
							.aka(new Gson().toJson(s.getAka()))
							.year(s.getYear())
							.rating(new Gson().toJson(s.getRating()))
							.genres(new Gson().toJson(s.getGenres()))
							.countries(new Gson().toJson(s.getCountries()))
							.casts(new Gson().toJson(s.getCasts()))
							.directors(new Gson().toJson(s.getDirectors()))
							.images(new Gson().toJson(s.getImages()))
							.summary(s.getSummary())
							.build()
						);
				}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private String read() {
		String encoding = "UTF-8";  
		File file = new File("C:\\Users\\Eric\\Desktop\\direct.txt");  
		Long filelength = file.length();  
		byte[] filecontent = new byte[filelength.intValue()];  
		try {  
			FileInputStream in = new FileInputStream(file);  
			in.read(filecontent);  
			in.close();  
		} catch (FileNotFoundException e) {  
			e.printStackTrace();  
		} catch (IOException e) {  
			e.printStackTrace();  
		}  
		try {  
			return new String(filecontent, encoding);  
		} catch (UnsupportedEncodingException e) {  
			System.err.println("The OS does not support " + encoding);  
			e.printStackTrace();  
			return null;  
		}  
	}
	
	private String unicode(String source){  
        StringBuffer sb = new StringBuffer();  
        char [] source_char = source.toCharArray();  
        String unicode = null;  
        for (int i=0;i<source_char.length;i++) {  
            unicode = Integer.toHexString(source_char[i]);  
            if (unicode.length() <= 2) {  
                unicode = "00" + unicode;  
            }  
            sb.append("\\u" + unicode);  
        }  
        return sb.toString();  
    }  
}
